package com.cpg.school.controller.service;

import com.cpg.school.mybatis.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @PostMapping("/student_answers_submit.do")
    public String student_answers_submit(HttpSession session, HttpServletRequest request) {
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> param = new ArrayList<>();
        Enumeration pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = (String) pNames.nextElement();
            String value = request.getParameter(name);
            Map map = new HashMap();
            map.put("uid", user_id);
            map.put("qid", name.replace("answers", ""));
            map.put("answer", value);
            if (!name.equals("question_ids")) param.add(map);
        }
        int add = studentMapper.student_answers_submit(param);
        return add > 0 ? "提交成功" : "提交失败";
    }
}
